package Server;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.codehaus.jettison.json.JSONObject;

import Server.BusinessObjects.Category;
import Server.BusinessObjects.Scenario;

@Path("/categories")
public class CategoryResource {


	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{scenarioId}")
	public String getCategoriesToScenario(
			@PathParam("scenarioId") int scenarioId) {

		List<JSONObject> objects = new ArrayList<JSONObject>();
		try {
			List<Category> categories = Scenario
					.getCategoriesToScenario(scenarioId);

			for (Category c : categories) {
				JSONObject o = new JSONObject();
				o.put("Id", c.getId());
				o.put("Name", c.getName());

				objects.add(o);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return objects.toString();
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/getAllCategories")
	public String getAllCategories() {

		List<JSONObject> objects = new ArrayList<JSONObject>();
		try {
			List<Category> categories = Category.getAllCategories();

			for (Category c : categories) {
				JSONObject o = new JSONObject();
				o.put("Id", c.getId());
				o.put("Name", c.getName());

				objects.add(o);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return objects.toString();
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/getAllCategoriesWithScenarios")
	public String getAllCategoriesWithScenarios() {

		List<JSONObject> objects = new ArrayList<JSONObject>();
		try {
			List<Category> categories = Category.getAllCategories();

			for (Category c : categories) {
				JSONObject o = new JSONObject();
				o.put("Id", c.getId());
				o.put("Name", c.getName());

				List<Scenario> scenarios = Category.getScenariosToCategory(c
						.getId());
				int i = 1;
				for (Scenario s : scenarios) {
					o.put("ScenarioId", s.getId());
					o.put("ScenarioName", s.getName());
					i++;
				}

				objects.add(o);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return objects.toString();
	}

}
